

#include "sp_scripter.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "log.h"


static Http* g_http = NULL;

typedef void (* lpfnRun)(const std::string& localfile, const Http::Param& para, lpfnReply reply);

static void reply(int code, const char * buf, int len)
{
    g_http->Reply(code, buf, len);
}

void SpScripter::run(Http* http, const std::string& localfile, const Http::Param& para) const
{
    g_http = http;
    RUN_HERE() << "localfile = " << localfile;
    void * handle = dlopen(localfile.c_str(), RTLD_LAZY);
    if (!handle) {
        RUN_HERE() << dlerror();
        http->Reply404();
        return;
    }
    lpfnRun lrun = (lpfnRun)dlsym(handle, "run");
    if (!lrun) {
        RUN_HERE();
        dlclose(handle);
        http->Reply404();
        return;
    }
    lrun(localfile, para, reply);
    dlclose(handle);
}



